/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import com.opensymphony.xwork2.ActionSupport;
import dao.CategoryDAO;
import collection.Categories;
import dto.Category;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLStreamException;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import util.HibernateUtil;
import util.XMLUtil;

/**
 *
 * @author duonghung1369
 */
public class BaseController extends ActionSupport {

    private List<Category> categories;

    public BaseController() {
        //loadSideBar();
    }

    protected String loadSideBar() {
        try {
            Session session = HibernateUtil.openSession();
            CategoryDAO categoryDAO = new CategoryDAO(Category.class);
            categories = categoryDAO.lazyLoadCategories(session);

            // init child categories before close session
            for (int i= 0; i < categories.size(); i++) {
                Hibernate.initialize(categories.get(i).getCategories());
            }

            session.flush();
            session.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * Not used temporary.
     */
    private void marshalCategories() {
        try {
            String filePath = System.getProperty("rootPath") + util.StringUtil.XML_FOLDER_PATH + "category.xml";
            JAXBContext context = JAXBContext.newInstance(Categories.class);
            Categories cts = new Categories(categories);
            XMLUtil.marshalObject(context, cts, filePath);
            System.out.println("Marshal category successfull at " + filePath);
        } catch (XMLStreamException ex) {
            Logger.getLogger(BaseController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(BaseController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JAXBException ex) {
            Logger.getLogger(BaseController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Not used temporary.
     */
    private void unmarshalCategories(){
        try {
            String filePath = System.getProperty("rootPath") + util.StringUtil.XML_FOLDER_PATH + "category.xml";
            JAXBContext context = JAXBContext.newInstance(Categories.class);
            Object obj = XMLUtil.unmarshalObject(context, filePath);
            if(obj != null){
                Categories cts = (Categories) obj;
            }
            System.out.println("Unmarshalling successfull!");
        } catch (JAXBException ex) {
            Logger.getLogger(BaseController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @return the categories
     */
    public List<Category> getCategories() {
        return categories;
    }

    /**
     * @param categories the categories to set
     */
    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }
}
